!---------------------------------------------------------------------------


subroutine node_sendanl2(vtype)
  !     Send just the 'type' variables back to the master process

  use node_mod, only: &
       mynum,         & ! INTENT(IN)
       master_num       ! INTENT(IN)
  use mem_grid, only: &
       ngrids           ! INTENT(IN)
  use var_tables, only: &
       num_var,         & ! INTENT(IN)
       vtab_r             ! INTENT(IN)

  implicit none
  ! Arguments:
  character(len=*), intent(in) :: vtype
  ! Local Variables:
  integer, parameter :: ntags=5  !4 ! Changed by ALF
  integer :: msgtags(ntags)
  integer :: msgnum,nv,isend,ng
  real, pointer :: v_pointer
  integer :: ipos, max_size, nwords
  real, allocatable :: buff(:)

  ! New TAG - ALF
  if     (vtype == 'MEAN') then
     msgnum=1234
  elseif (vtype == 'LITE') then
     msgnum=1235
  elseif (vtype == 'BOTH') then
     msgnum=1236
  else
     return
  endif

  msgtags(1)=mynum

  do ng=1,ngrids

     msgtags(2)=ng

     do nv=1, num_var(ng)

        isend=0
        if(vtype == 'LITE' .and. vtab_r(nv,ng)%ilite == 1) then
           isend=1
           v_pointer => vtab_r(nv,ng)%var_p
        elseif( (vtype == 'MEAN' .or. vtype == 'BOTH')  &
             .and. vtab_r(nv,ng)%imean == 1) then
           isend=1
           v_pointer => vtab_r(nv,ng)%var_m
        endif

        if(isend == 1) then

           msgtags(5)= nv
           msgtags(4)= vtab_r(nv,ng)%idim_type
           msgtags(3)= vtab_r(nv,ng)%npts

           ! Prepare the buffer
           nwords = ntags + vtab_r(nv,ng)%npts
           call parf_pack_max_size(nwords, max_size)
           allocate(buff(max_size))

           ipos = 1

           ! Add the array of integer (msgtags) to the buffer
           call parf_pack_int(msgtags, ntags, buff, max_size, ipos)

           ! Add the array of real to the buffer
           call parf_pack_real(v_pointer, vtab_r(nv,ng)%npts, &
                buff, max_size, ipos)

           ! Send the message
           call parf_send_block(buff, max_size, master_num, msgnum)

           deallocate(buff)

        endif

     enddo

  enddo

end subroutine node_sendanl2

!     ****************************************************************


subroutine master_getanl2(vtype)
  !     Get just the 'type' variables from the nodes

  use mem_grid, only: &
       ngrids,        & ! INTENT(IN)
       nnzp,          & ! INTENT(IN)
       nnxp,          & ! INTENT(IN)
       nnyp,          & ! INTENT(IN)
       nzg,           & ! INTENT(IN)
       npatch,        & ! INTENT(IN)
       nzs              ! INTENT(IN)
  use rpara, only: &
       NMACHS,     & ! INTENT(IN)
       NXBEGC,     & ! INTENT(IN)
       NXENDC,     & ! INTENT(IN)
       NYBEGC,     & ! INTENT(IN)
       NYENDC,     & ! INTENT(IN)
       IBCFLG,     & ! INTENT(IN)
       NXEND,      & ! INTENT(IN)
       NXBEG,      & ! INTENT(IN)
       NYEND,      & ! INTENT(IN)
       NYBEG,      & ! INTENT(IN)
       IXOFF,      & ! INTENT(IN)
       IYOFF         ! INTENT(IN)
  use mem_scratch, only: &
       scratch       ! INTENT(INOUT)
  use var_tables, only: &
       num_var,         & ! INTENT(IN)
       vtab_r             ! INTENT(IN)

  ! For CATT

  use mem_aerad, only: nwave

  implicit none
  ! Arguments:
  character(len=*), intent(in) :: vtype
  ! Local Variables:
  integer, parameter :: ntags=5  !4 ! Changed by ALF
  integer :: msgtags(ntags)
  integer :: numvars, ng, nummess, nvvv, ihostnum
  integer :: nm, il1, ir2, jb1, jt2, mxp, myp
  integer :: nv, npts, idim_type
  real, pointer :: v_pointer
  integer :: msgnum
  integer :: ipos, max_size, nwords, maxz, maxx, maxy
  real, allocatable :: buff(:)

  if     (vtype == 'MEAN') then
     msgnum=1234
  elseif (vtype == 'LITE') then
     msgnum=1235
  elseif (vtype == 'BOTH') then
     msgnum=1236
  else
     return
  endif

  ! Checking the amount of information (messages) to be received
  numvars = 0
  do ng = 1, ngrids
     do nv = 1, num_var(ng)
        if (vtype == 'MEAN' .or. vtype == 'BOTH' ) then
           if (vtab_r(nv,ng)%imean == 1) numvars=numvars+1
        elseif (vtype == 'LITE') then
           if (vtab_r(nv,ng)%ilite == 1) numvars=numvars+1
        endif
     enddo
  enddo
  nummess = numvars*nmachs ! nummess = number of messages to be received

  maxz   = maxval(nnzp(1:ngrids))
  maxx   = maxval(nnxp(1:ngrids))
  maxy   = maxval(nnyp(1:ngrids))
  nwords = maxz*maxx*maxy
  call parf_pack_max_size(nwords, max_size)
  allocate(buff(max_size))

  ! Receiving the messages
  do nvvv = 1, nummess

     ! Receive one message and save the information in buffer: scratch%scr1
     call parf_get_block_any(buff, max_size, msgnum, ihostnum)

     ! Identify the integer array from the buffer
     call parf_unpack_int(buff, max_size, ipos, msgtags, ntags)

     nm        = msgtags(1)   ! Number of process for the message received
     ng        = msgtags(2)   ! Number of the Grid
     npts      = msgtags(3)   ! Total number of points for the real array
     idim_type = msgtags(4)   ! Dimension of the real array
     nv        = msgtags(5)   ! Number of the variable in: vtab_r(nv,ng)

     if (vtype == 'MEAN' .or. vtype == 'BOTH' ) then
        v_pointer => vtab_r(nv,ng)%var_m
     elseif (vtype == 'LITE') then
        v_pointer => vtab_r(nv,ng)%var_p
     endif

     ! Calculating the position (plane coordinates) to fit on grid
     il1=nxbegc(nm,ng)
     ir2=nxendc(nm,ng)
     jb1=nybegc(nm,ng)
     jt2=nyendc(nm,ng)

     if(iand(ibcflg(nm,ng),1).ne.0) il1=il1-1
     if(iand(ibcflg(nm,ng),2).ne.0) ir2=ir2+1
     if(iand(ibcflg(nm,ng),4).ne.0) jb1=jb1-1
     if(iand(ibcflg(nm,ng),8).ne.0) jt2=jt2+1

     mxp = nxend(nm,ng) - nxbeg(nm,ng) + 1
     myp = nyend(nm,ng) - nybeg(nm,ng) + 1

     ! Identify the real array from the buffer
     call parf_unpack_real(buff, max_size, ipos, scratch%scr1(1), npts)

     ! Copying the buffer scratch in the real array
     if (idim_type == 2) then
        call ex_2_buff(v_pointer,scratch%scr1(1)  &
             ,nnxp(ng),nnyp(ng)  &
             ,mxp,myp  &
             ,ixoff(nm,ng),iyoff(nm,ng),il1,ir2,jb1,jt2)
     elseif (idim_type == 3) then
        call ex_3_buff(v_pointer,scratch%scr1(1)  &
             ,nnzp(ng),nnxp(ng),nnyp(ng)  &
             ,nnzp(ng),mxp,myp  &
             ,ixoff(nm,ng),iyoff(nm,ng),il1,ir2,jb1,jt2)
     elseif (idim_type == 4) then
        call ex_4_buff(v_pointer,scratch%scr1(1)  &
             ,nzg,nnxp(ng),nnyp(ng),npatch  &
             ,nzg,mxp,myp,npatch  &
             ,ixoff(nm,ng),iyoff(nm,ng),il1,ir2,jb1,jt2)
     elseif (idim_type == 5) then
        call ex_4_buff(v_pointer,scratch%scr1(1)  &
             ,nzs,nnxp(ng),nnyp(ng),npatch  &
             ,nzs,mxp,myp,npatch  &
             ,ixoff(nm,ng),iyoff(nm,ng),il1,ir2,jb1,jt2)
     elseif (idim_type == 6) then
        call ex_2p_buff(v_pointer,scratch%scr1(1)  &
             ,nnxp(ng),nnyp(ng),npatch  &
             ,mxp,myp,npatch  &
             ,ixoff(nm,ng),iyoff(nm,ng),il1,ir2,jb1,jt2)
     elseif (idim_type == 7) then
        call ex_buff_carma(vtab_r(nv,ng)%var_p,scratch%scr1(1)  &
             ,nnxp(ng),nnyp(ng),nwave,mxp,myp,nwave  &
             ,ixoff(nm,ng),iyoff(nm,ng),il1,ir2,jb1,jt2)
     endif
  enddo

  deallocate(buff)

end subroutine master_getanl2

